﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Data
{
    public class Song
    {
        #region Объектная модель
        static protected readonly Dictionary<Guid, Song> _allSongs = new Dictionary<Guid, Song>();
        static public Dictionary<Guid, Song> AllSongs { get { return _allSongs; } }

        protected Guid _id { get; private set; }
        public Guid ID
        {
            get { return _id; }
        }

        static public Song GetById(Guid id)
        {
            return _allSongs[id];
        }
        #endregion

        static private readonly String _tableName = "[songs]";
        static public String TableName
        {
            get { return _tableName; }
        }

         public Song()
        {
            _id = Guid.NewGuid();
            _allSongs.Add(this._id, this);
        }

         static public DataTable AllSongsFromDB
         {
             get
             {
                 DataTable dt = new DataTable();
                 String query = "SELECT * FROM [songs]";

                 // Connect to the database and run the query.        
                 SqlDataAdapter adapter = new SqlDataAdapter(query, DBManager.Instance.Connection);

                 // Fill the DataSet.
                 adapter.Fill(dt);

                 return dt;
             }
             set
             {
                 DataTable dt = new DataTable();
                 String query = "SELECT * FROM [songs]";

                 // Connect to the database and run the query.        
                 SqlDataAdapter adapter = new SqlDataAdapter(query, DBManager.Instance.Connection);
                 // Create a command builder to generate SQL update, insert, and
                 // delete commands based on selectCommand. These are used to
                 // update the database.
                 SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adapter);

                 // Fill the DataSet.
                 adapter.Update(value);
             }
         }
    }
}
